﻿#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QMessageBox>
#include <QTextCodec>
#include <iostream>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(checkAppStatus()));
    process = new QProcess(this);
    trayIcon = new QSystemTrayIcon(this);
    menu = new QMenu((QWidget*)QApplication::desktop());
    quitAction = new QAction("退出(&Q)",this);
    connect(quitAction, SIGNAL(triggered(bool)), qApp, SLOT(quit()));
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(show()));

}

Widget::~Widget()
{
    delete ui;
    delete trayIcon;
}

bool Widget::cmdCtrl(QMap<QString, QString> data)
{
    //[-h] [-n] [-p] [-t] [-c]
    QString name = data.value("AppName");
    QString path = data.value("path");
    int ms = data.value("msec").toInt();
    QString _code = data.value("code");

    if(ms<100){
        ms = 100;
    }
    msec = ms;
    exePath = path;
    code = _code;
    proName = name;

    if((proName=="")&&(exePath!="")&&exePath.contains("\\")){
        if(exePath.contains("/")){
            std::cout << "path error,please input right path" << std::endl;
            return false;
        }
        QStringList listExePath;
        listExePath = exePath.split("\\");
        appName = listExePath.at(listExePath.length()-1);

    }
    else if((proName=="")&&(exePath!="")&&exePath.contains("/")){
        if(exePath.contains("\\")){
            std::cout << "path error,please input right path" << std::endl;
            return false;
        }
        QStringList listExePath;
        listExePath = exePath.split("/");
        appName = listExePath.at(listExePath.length()-1);
    }
    else if(proName!=""){
        appName = proName;
    }



    timer->start(msec);

    if(timer->isActive()){
        return true;
    }
    return false;
}

bool Widget::checkAppStatus()
{
//    std::cout << "checkAppStatus is running" << std::endl;
    #ifdef Q_OS_WIN
    process->start("tasklist", QStringList() << "/FI" << "IMAGENAME eq " + appName);
    process->waitForFinished(5000);

    if(code == "GBK" || code == "GB2312"){
        QTextCodec *codec = QTextCodec::codecForName("GBK");
        QTextCodec::setCodecForLocale(codec);
    }
    else{
        QTextCodec *codec = QTextCodec::codecForName("UTF-8");
        QTextCodec::setCodecForLocale(codec);
    }

    QString outputStr;
    outputStr = QString::fromLocal8Bit(process->readAllStandardOutput());
//    qDebug() << "output:" << outputStr;
    if(outputStr.contains(appName)){
//        qDebug("process is running");
        process->close();
        return true;
    }
    else{
//        qDebug("process is close");
        QProcess process;
        QStringList arguments;
        if(exePath==""){
            arguments << "/c" << exePath;
        }
        else{
            arguments << "/c" << "start" << exePath;
        }

        process.start("cmd", arguments);
        process.waitForFinished();
    }
//    qDebug() << "error:" << process->readAllStandardError();
    process->close();

    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codec);
    return false;
    #endif
}


bool Widget::on_pushButton_clicked()
{
    proName = ui->lineEdit_2->text();
    exePath = ui->lineEdit->text();
    if((proName=="")&&(exePath!="")&&exePath.contains("\\")){
        if(exePath.contains("/")){
            QMessageBox::warning(this, "警告", "请确保路径正确");
            return false;
        }
        QStringList listExePath;
        listExePath = exePath.split("\\");
        appName = listExePath.at(listExePath.length()-1);

    }
    else if((proName=="")&&(exePath!="")&&exePath.contains("/")){
        if(exePath.contains("\\")){
            QMessageBox::warning(this, "警告", "请确保路径正确");
            return false;
        }
        QStringList listExePath;
        listExePath = exePath.split("/");
        appName = listExePath.at(listExePath.length()-1);
    }
    else if(proName!=""){
        appName = proName;
    }
    else{
        QMessageBox::warning(this, "警告", "主程序路径为空或路径错误！");
        return false;
    }

    msec = ui->lineEdit_3->text().toInt();
    if(msec<100){
        msec = 100;
    }
    qDebug() << "appName:" << appName << "msec:" << msec;

    static bool flagLook = false;
    if(!flagLook){
        ui->pushButton->setText("停止监视");
        ui->label_7->setStyleSheet("background-image: url(:/img/TaskMan_open.png);");
        ui->label_7->setToolTip("正在监视哦");
        timer->start(msec);
        flagLook = true;
    }
    else {
        ui->pushButton->setText("开始监视");
        ui->label_7->setStyleSheet("background-image: url(:/img/TaskMan_win.png);");
        ui->label_7->setToolTip("停止了监视了诶");
        timer->stop();
        flagLook = false;
    }
    return true;

}

void Widget::on_pushButton_2_clicked()
{
    if(!QSystemTrayIcon::isSystemTrayAvailable()){
        return;
    }
//    menu->addAction("Action 1");
//    menu->addAction("Action 2");
//    menu->addSeparator();
    menu->addAction(quitAction);

    trayIcon->setIcon(QIcon(":/img/icon.ico"));
    trayIcon->setToolTip("TaskMan");
    trayIcon->setContextMenu(menu);
    trayIcon->show();

}

void Widget::on_comboBox_activated(const QString &arg1)
{
    code = ui->comboBox->currentText();
}
